package com.li.springbootproject.temptableuse.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.li.springbootproject.domain.User;
import com.li.springbootproject.domain.common.BaseConst;
import com.li.springbootproject.mapper.CommonMapper;
import com.li.springbootproject.temptableuse.mapper.MysqlMapper;
import com.li.springbootproject.temptableuse.service.MysqlService;
import com.lidong.toolkit.utils.ListUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 10/12/2022 11:06 AM
 */
@Service
public class MysqlServiceImpl implements MysqlService {

    private static final Logger logger = LoggerFactory.getLogger(MysqlServiceImpl.class);

    @Autowired
    private MysqlMapper mysqlMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CommonMapper commonMapper;

    /**
     * Mysql 临时表使用测试
     */
    @Override
    public void tempTableInsert() {
        // 创建临时表
        String createSql = "create temporary  TABLE  IF NOT EXISTS `tempuser` (\n" +
                "`id` varchar(36) ,\n" +
                "`username` varchar(100) ,\n" +
                "`password` varchar(100)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=latin1;";
        mysqlMapper.executeSql(createSql);
        // 往临时表写数据
        List<User> userList = getUserList();
        List<List<User>> lists = ListUtils.splitList(userList, BaseConst.TEN_THOUSAND);
        for (List<User> list : lists) {
            mysqlMapper.insertBatch(list);
        }
        List<Map<String, Object>> list1 = mysqlMapper.listSql("select * from tempuser");
        // 使用临时表查询数据
        List<Map<String, Object>> list2 = mysqlMapper.listSql("select * from tempuser left join t_car on tempuser.id = t_car.id");
        List<Map<String, Object>> list3 = commonMapper.listSql("select * from tempuser left join t_car on tempuser.id = t_car.id");
        queryTest1();
        // 删除临时表
        String dropSql = "drop table if exists tempuser;";
        mysqlMapper.executeSql(dropSql);

    }

    private void queryTest1() {
        List<Map<String, Object>> list1 = mysqlMapper.listSql("select * from tempuser");
        // 使用临时表查询数据
        List<Map<String, Object>> list2 = mysqlMapper.listSql("select * from tempuser left join t_car on tempuser.id = t_car.id");
        List<Map<String, Object>> list3 = commonMapper.listSql("select * from tempuser left join t_car on tempuser.id = t_car.id");
        logger.info("queryTest1");
    }

    /**
     * 获取 10 万用户
     *
     * @return List
     */
    private List<User> getUserList() {
        List<User> userList = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setUsername("user" + i);
            user.setPassword("user" + i);
            userList.add(user);
        }
        return userList;
    }
}
